package com.itheima.homework.uuid;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.util.UUID;

public class mothed implements Runnable{
    private Socket accept;

    public mothed(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(accept.getInputStream());
            UUID uuid = UUID.randomUUID();
            String s = uuid.toString().replace("-", "");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    new FileOutputStream(new File("Day15_Socket", s + ".jpg")));
            int b;
            while ((b = bufferedInputStream.read()) != -1) {
                bufferedOutputStream.write(b);
            }
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bufferedWriter.write("在" + LocalDateTime.now() + "上传成功!");
            bufferedWriter.flush();
            accept.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
